<?php
    function CloseExpiredDiscussions (&$DiscussionGrid) {
            $sql = $DiscussionGrid->Context->ObjectFactory->NewContextObject($DiscussionGrid->Context, 'SqlBuilder');
            $sql->SetMainTable('Discussion','d');
            $sql->AddFieldNameValue('Closed', 1, 1);
            $sql->AddWhere('d','Closed','','0','=','', '', 1, 1);
            $sql->AddWhere('d','DateExpire','','0000-00-00','>','and', '', 1, 0);
            $sql->AddWhere('d','DateExpire','','NOW()','<','and', '', 0, 0);
            $sql->AddWhere('d','Active','','1','=','and', '', 1, 0);
            $sql->EndWhereGroup();
            $result = $DiscussionGrid->Context->Database->Update($sql, $DiscussionGrid, 'CloseExpiredDiscussions', 'An error occurred while closing expired discussions.');
        }
    function Render_DateExpireForm (&$DiscussionForm) {
        global $Context;
        if ($DiscussionForm->Discussion->DiscussionID > 0) {  //Editing existing Discussion
            $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
            $sql->SetMainTable('Discussion','d');
            $sql->AddSelect('DateExpire', 'd');
            $sql->AddWhere('d','DiscussionID','',$DiscussionForm->DiscussionID,'=');
            $result = $Context->Database->Select($sql, $DiscussionForm, 'Render_DateExpireForm', 'An error occurred while listing the discussion information.');
            $rows = $Context->Database->GetRow($result);
            if ($rows[0] == "0000-00-00") $DateExpire = "";
            else $DateExpire = FormatDateToPhp($rows[0]);
        }
        else $DateExpire = ""; //New Discussion

        echo '<li class="DiscussionExpiration">
              <label>'.$Context->GetDefinition('DiscussionExpiresOn').'</label>
              <input name="DateExpire" id="DateExpire" class="date-pick dp-applied" value="'.$DateExpire.'"/><br style="clear:both"/>
              <span>'.$Context->GetDefinition('DateType').'</span>
              </li>';
    }
    function DiscussionForm_CheckDateExpire (&$DiscussionForm) {
        $DateExpireString = ForceIncomingString('DateExpire', '');
        if ($DateExpireString != "") {
            CheckDateFormat(date("m/d/Y",strtotime($DateExpireString)));
        }
    }
    function DiscussionForm_SaveDateExpire (&$DiscussionForm) {
        $DateExpire = ForceIncomingString('DateExpire', '');
        $ResultDiscussion = $DiscussionForm->DelegateParameters['ResultDiscussion'];
        if ($ResultDiscussion) {
            $sql = $DiscussionForm->Context->ObjectFactory->NewContextObject($DiscussionForm->Context, 'SqlBuilder');
            $sql->SetMainTable('Discussion', 'd');
            if ($DateExpire == "")
                $sql->AddFieldNameValue('DateExpire', "0000-00-00");
            else {
                $DateExpire = date("m/d/Y",strtotime($DateExpire));
                $sql->AddFieldNameValue('DateExpire', FormatStringForDatabaseInput(FormatDateToMysql($DateExpire)));
            }
            $sql->AddWhere('d', 'DiscussionID', '', $ResultDiscussion->DiscussionID, '=');
            $DiscussionForm->Context->Database->Update($sql, $DiscussionForm, 'DiscussionForm_SaveDateExpire', 'An error occurred while updating discussion properties.');
        }
    }
    function FormatDateToPhp($date)
    {
        list($yyyy, $mm, $dd) = explode('-',$date);
        $date = date('m/d/Y', mktime(0,0,0,$mm,$dd,$yyyy));
        return $date;
    }
    function FormatDateToMysql($date)
    {
        list($mm, $dd, $yyyy) = explode('/',$date);
        $date = $yyyy.'-'.$mm.'-'.$dd;
        return $date;
    }
    function CheckDateFormat($strdate)
    {
        global $Context;
        $rr = strtotime($strdate);
        $pp = strtotime("now");
        if (strtotime($strdate) < strtotime("now")-86400) {
            $Context->WarningCollector->Add($Context->GetDefinition('ErrWrongDate'));
            return 0;
        }
        else return 1;
    }


?>
